package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProListMapper;
import com.indusfo.spc.pojo.ProList;
import com.indusfo.spc.pojo.Station;
import com.indusfo.spc.service.ProListService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 工序业务层
 *
 * @author xuz
 * @date 2018/11/23 2:30 PM
 */
@Service
public class ProListServiceImpl implements ProListService {

    private  static  final Logger logger = LoggerFactory.getLogger(ProListServiceImpl.class);

    @Resource
    private ProListMapper proListMapper;

    @Override
    public JSONObject insertProList(Integer proId, Integer[] staIds, Integer dataState) {
        return null;
    }

    @Override
    public JSONObject deleteProList(Integer proId, Integer[] staIds, Integer dataState, HttpServletRequest request) {
        try {
            if(staIds==null){
                throw new ParamsErrorException("请选择工位");
            }
            /**
             * @author: louk
             * @Description:获取到session,然后判断当前要删除的是session当中的还是数据库当中的
             * @date: 2019/9/11 19:00
             */
            //初始化一个空的集合用来存储真正要访问数据库的
            List<Integer> trueDelete=new ArrayList<>();
            //获取到session当中的集合
            HttpSession session = request.getSession();
            List<Integer> staIds1 = (List<Integer>)session.getAttribute("staIds");
            //如果session当中的集合不为空的话
            if(staIds1!=null){
                if(!staIds1.isEmpty()){
                //将传递的数据与session当中的进行比较
                for (Integer staId : staIds) {
                        //如果当前session中存在则进行remove
                        if(staIds1.contains(staId)){
                            //如果包含的话直接remove掉
                            staIds1.remove(Integer.valueOf(staId));
                            session.setAttribute("staIds",staIds1);
                        }else{
                            //不包含的话说明要进行数据库删除
                            trueDelete.add(staId);
                        }
                    }
                }
                //如果真实删除的数据不为空时才进行删除
                if(trueDelete.isEmpty()){
                    return JSONObject.oK("删除成功");
                }
                //初始化一个真实的要进行数据库删除的数组
                Integer[] newArray=new Integer[trueDelete.size()];
                //对真实要删除的集合的数据拷贝到数组当中
                for (int i = 0; i < trueDelete.size(); i++) {
                    newArray[i]=trueDelete.get(i);
                }
                int row = proListMapper.deleteProList(proId, newArray, dataState);
                //int row = proListMapper.deleteProList(proId, staIds, dataState);
                if(row==0){
                    throw new ParamsErrorException("删除失败");
                }
            }else{
                int row = proListMapper.deleteProList(proId, staIds, dataState);
                //int row = proListMapper.deleteProList(proId, staIds, dataState);
                if(row==0){
                    throw new ParamsErrorException("删除失败");
                }
            }
            return JSONObject.oK("删除成功");
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }

    }

    @Override
    public JSONObject queryProList(ProList proList) {
        List<ProList> listProList = null;
        try {
            Integer pagesize = proList.getPagesize();
            Integer pageindex = proList.getPageindex();
            if(pagesize != null && pageindex != null) {
                proList.setIncept(pagesize*(pageindex - 1));
            }
            listProList = proListMapper.queryProList(proList);

            if (listProList.isEmpty()) {
                return JSONObject.oK("没有相关工序数据", listProList, 0);
            }
            //查询分页总数
            int count = proListMapper.countProList(proList);
            return JSONObject.oK("查询成功", listProList, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }
    /**
     * @author: louk
     * @Description:根据工序ID查找工位内容
     * @date: 2019/9/11 17:53
     *
     */
    @Override
    public JSONObject queryStationByProList(ProList proList) {
        List<Station> stations=null;
        try {
            if(proList.getProId()==null){
                throw new ParamsErrorException("请选择工序");
            }
            Integer pagesize = proList.getPagesize();
            Integer pageindex = proList.getPageindex();
            if(pagesize != null && pageindex != null) {
                proList.setIncept(pagesize*(pageindex - 1));
            }
            stations = proListMapper.queryStationByProList(proList);

            if(stations.isEmpty()){
                return JSONObject.oK("没有工位相关数据", stations, 0);
            }
            int i = proListMapper.countProList(proList);
            return JSONObject.oK("查询成功", stations, i);
        } catch (GlobalException e) {
            // 捕获异常,打印并返回
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }


    /**
     * @author: louk
     * @Description: 根据工序ID,产线,车间查找工位
     * @date: 2019/9/11 17:57
     *
     */
    @Override
    public JSONObject queryStationBydisSta(Integer[] staIds, Integer pagesize, Integer pageindex, Integer proId, Integer workshopId, Integer productionLineId, HttpServletRequest request) {

        try {
            Integer lN=null;
            /*if(workshopId==null){
                throw new ParamsErrorException("请选择车间");
            }*/
            if(productionLineId==null){
                throw new ParamsErrorException("请选择产线");
            }
            if(pagesize != null && pageindex != null) {
                lN=pagesize*(pageindex - 1);
            }
            if(staIds!=null){
                if(staIds.length==0){
                    staIds=null;
                }
            }

            List<Station> stations = proListMapper.queryStationBydisSta(pagesize,pageindex,lN,proId, workshopId, productionLineId,staIds);
            if (stations.isEmpty()){
                return JSONObject.oK("没有工位相关数据", stations, 0);
            }

            int row = proListMapper.countStationBydisSta(proId, workshopId, productionLineId,staIds);



            return JSONObject.oK("查询成功", stations, row);
        }catch (GlobalException e) {
            // 捕获异常,打印并返回
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }
    /*@Override
    public JSONObject queryStationBydisSta(Integer[] staIds,Integer pagesize,Integer pageindex,Integer proId,Integer workshopId,Integer productionLineId,HttpServletRequest request) {

        try {
            Integer lN=null;
            *//*if(workshopId==null){
                throw new ParamsErrorException("请选择车间");
            }*//*
            if(productionLineId==null){
                throw new ParamsErrorException("请选择产线");
            }
            if(pagesize != null && pageindex != null) {
                lN=pagesize*(pageindex - 1);
            }
            HttpSession session = request.getSession();
            List<Integer> sessionstaIds = (List<Integer>)session.getAttribute("staIds");


                List<Station> stations = proListMapper.queryStationBydisSta(pagesize,pageindex,lN,proId, workshopId, productionLineId,sessionstaIds);
                if (stations.isEmpty()){
                    return JSONObject.oK("没有工位相关数据", stations, 0);
                }

                int row = proListMapper.countStationBydisSta(proId, workshopId, productionLineId,sessionstaIds);



            return JSONObject.oK("查询成功", stations, row);
        }catch (GlobalException e) {
            // 捕获异常,打印并返回
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }
    }*/

    @Override
    public JSONObject insertSession(Integer[] staIds, HttpServletRequest request) {
        try {
            if(staIds!=null){
                List<Integer> newSession=new ArrayList<Integer>();
                for (Integer staId : staIds) {
                    newSession.add(staId);
                }
                HttpSession session = request.getSession();
                if(session.getAttribute("staIds")==null){
                    session.setAttribute("staIds",newSession);
                }else{
                    List<Integer> newSessions = (List<Integer>)session.getAttribute("staIds");
                    newSessions.addAll(newSession);
                    session.setAttribute("staIds",newSessions);
                }
            }
            return JSONObject.oK("成功");
        } catch (GlobalException e) {
            // 捕获异常,打印并返回
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());
        }

    }


}
